package org.sacinfo.exception;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: CustomExceptionResolver</p>
 * <p>Description: 自定义异常处理器</p>
 * @version 1.0
 */
public class CustomExceptionResolver implements HandlerExceptionResolver  {
	
	private Logger logger =Logger.getLogger(CustomExceptionResolver.class);

	//前端控制器DispatcherServlet在进行HandlerMapping、调用HandlerAdapter执行Handler过程中，如果遇到异常就会执行此方法
	//handler最终要执行的Handler，它的真实身份是HandlerMethod
	//Exception ex就是接收到异常信息
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		ex.printStackTrace();
		logger.error(ex);
		//统一异常处理代码
		//String message = null;
		CustomException customException = null;
		if(ex instanceof CustomException){
			customException = (CustomException)ex;
		}else{
			customException = new CustomException("未知错误");
		}
		
		//错误 信息
		//message = customException.getMessage();
		 Map<String, Object> model = new HashMap<String, Object>();  
         model.put("ex", customException);  
		 return new ModelAndView("error",model);
	}

}
